package com.hk.Mgr.web.controller.app.yingmi;

/**
 * Created by lipei on 2017/9/25.
 */

import com.hk.api.exceptions.ValidateException;
import com.hk.app.utils.YingmiOpenAPI;
import com.hk.commons.UserProductConstant;
import com.hk.commons.entity.Answer;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.utils.MatchRuleUtil;
import com.hk.commons.vo.ProductUserRiskLevelResponse;
import com.hk.service.sys.IAnswer;
import com.hk.service.sys.ISysUser;
import com.hk.yingmi.YingmiOpenAPIConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lipei on 2017/9/25.
 */
@Component
public class FundProductHelper  {

    private Logger logger = LoggerFactory.getLogger(FundProductHelper.class);

    @Autowired
    private YingmiOpenAPI openAPI;

    public FundProductHelper() {
    }

    @Autowired
    ISysUser userService;

    @Autowired
    IAnswer iAnswer;

    /**
     * 匹配用户风险等级
     * @param fundRiskLevle  基金风险等级
     * @param userId  用户id
     * @return
     */
    public ProductUserRiskLevelResponse checkUserRiskLevel(String fundRiskLevle, String userId){
        ProductUserRiskLevelResponse productUserRiskLevelResponse = new ProductUserRiskLevelResponse();
        Long userIdLong = Long.valueOf(userId);
        SysUser user = userService.findById(userIdLong);
        Answer answer = iAnswer.getByUserId(userIdLong);

        if(user == null ) {
            throw new ValidateException("无效用户");
        }

        String userRiskLevel = null;

        long oldGroupId = 1L;
        Long groupId = oldGroupId;

        if (answer != null) {
            groupId = answer.getGroupId();
            userRiskLevel = answer.getScore();//用户等级
        }

        if(groupId.equals(oldGroupId)) {
            Map<String, String> params = new HashMap<>();
            params.put("accountName", user.getRealName().trim());
            params.put("identityNo", user.getIdentity());
            logger.info("parm Map = {}", params);
            String result = openAPI.sslGet_ByMap(YingmiOpenAPIConstant.ACCOUNT_GET_ACCOUNT_BY_IDENTITY, params, null);
            logger.info("result = {}", result);

            if(result.contains("\"riskGrade5\":\"null\"")) {
                productUserRiskLevelResponse.setEvaluatingAgain(true);
                return productUserRiskLevelResponse;
            }

            try {
                HashMap map = openAPI.convertToMap(result);
                if((map.get("riskGrade5") != null)) {
                    String riskGrade5 = map.get("riskGrade5").toString();
                    userRiskLevel = "C" +riskGrade5;

                    Object minRiskGrade = map.get("isMinRiskGrade");
                    logger.info("minRiskGrade Obj ={}",minRiskGrade);

                    if(minRiskGrade != null){
                        logger.info("minRiskGrade ={}",minRiskGrade.toString());

                        if (!"null".equals(minRiskGrade.toString())) {

                            Boolean isMinRiskGrade = (Boolean) minRiskGrade;
                            if(isMinRiskGrade) {
                                return  MatchRuleUtil.getMatchRule(UserProductConstant.USER_RISK_LEVEL_C1_1, fundRiskLevle);
                            }else {
                                return  MatchRuleUtil.getMatchRule(userRiskLevel, fundRiskLevle);
                            }
                        }

                    }

                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        return  MatchRuleUtil.getMatchRule(userRiskLevel, fundRiskLevle);
    }

}
